if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {
	var html = null;
	var thisPage = null;
	var user = null;
	var _currentloginuser = null;
	var orderID = [];
	var mainorderno = getUrlParam('mainorderno');
	var payChannels = {};

	var payCenterPage = function() {
		this.init();
	};

	payCenterPage.prototype = {
		init: function() {
			this.initData();
			this.bindEvent();
		},
		initData: function() {
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user) {
				redirect("../account/login.html");
				return;
			}
			if (!mainorderno) {
				$.toast('没有找到订单号');
				return;
			}
			//_currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
			$.when(
				$.ajax({
					type: "GET",
					url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id,
					async: true,
					contentType: 'application/json',
					headers: { "token": user.token },
				}),
				$.ajax({
					type: "GET",
					headers: {token: user.token},
					url: CONFIG_MANAGER.RequstUrl + "v1/orders/main/" + mainorderno + "/" + user.id,
					async: true,
					contentType: 'application/json',
					beforeSend:function(){ $.showWaiting(); }
				})
			).done(function(userResult, orderResult) {
				var thisInit = new payCenterInit();
				//初始化页面
				var orders = orderResult[0];
				thisInit.initOrderInfor(userResult[2].responseJSON, orders);				
				var i = 0;
				while (orders[i]){
					orderID[i] = orders[i].id;
					i++;
				}
			}).fail(function(data) {
				//$.toast(data.responseJSON.Message);
				$.closeWaiting();
			}).always(function(data) {
				$.closeWaiting();
			});

			//获取用户是否设置支付密码
			$.ajax({
				type: "GET",
				headers: {
					"token": user.token
				},
				url: CONFIG_MANAGER.RequstUrl + "v1/member/safety/paypasswd/" + user.id,
				async: true
			}).done(function(data) {
				setLocalStorage(STORAGE_MANAGER.issetpaypasswd, true);
			}).fail(function(data) {
				setLocalStorage(STORAGE_MANAGER.issetpaypasswd, false);
			});
			this.initPayEnviroment();
		},
		initPayEnviroment: function() {
			if (AGENT_CLIENT.APP) {
				plus.payment.getChannels(function(channels) {
					for (var i in channels) {
						var channel = channels[i];
						payChannels[channel.id] = channel;
					}
				}, function(e) {
					console.log("获取支付通道失败：" + e.message);
				});
			}
		},
		bindEvent: function() {
			var thisEvent = new payCenterEvent();
			//回退
			$("#body").on("click", "#back", function() {
				var selectRechargeAmount = getUrlParam('selectRechargeAmount');
				if(selectRechargeAmount){
					back();
				}else{
					if(AGENT_CLIENT.APP){
						plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(3)");
					}else{
						redirect("../shopcart/shoppingcart.html");
					}
				}
			});

			//点击支付
			$("#body").on("click", "#commitPay", function() {
				var type = $("#main input[name='paytype']:checked").val();
				var payFee = parseFloat($("#pay_fee").html());
				var money = parseFloat($("#money").html());
				var deliveryFee = parseFloat($("#delivery_fee").html());
				var isWalletChecked = $("#checkbox-1-1_wallet").is(":checked");
				var threedPayCheckedLength = $("input[name=paytype]:checked").length;
				var orderStr = orderID.join(',');
				if (isWalletChecked) {
					if (money >= (payFee + deliveryFee)) {
						var password = $("#payPassword #password").val();
						if ('' == password) {
							$.toast("请填写支付密码");
							return;
						}
						thisEvent.welletPay(orderStr, user, password, mainorderno);
					} else {
						$.toast("钱包余额不足");
					}
				} else if (threedPayCheckedLength > 0) {
					if (AGENT_CLIENT.APP) {
						console.log(JSON.stringify(payChannels));
						thisEvent.appPay(payChannels[type], orderStr, user.id, mainorderno);
					} else {
						thisEvent.webPay(type, orderStr, user.id, mainorderno);
					}
				}else{
					$.toast("请选择支付方式");
					return;
				}

			});

			//钱包支付和第三方支付切换
			thisEvent.changePayType();
			//显示隐藏订单详情
			thisEvent.showOrderDetail();
		}
	};

	thisPage = new payCenterPage();
}